package com.huawei.hianalytics.f.f;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import com.huawei.hianalytics.f.b.c;
import com.huawei.hianalytics.f.g.d;
import com.huawei.hianalytics.f.g.h;
import com.huawei.hianalytics.f.g.i;
import com.huawei.hianalytics.f.g.k;
import com.huawei.hianalytics.g.b;
import com.huawei.hianalytics.i.a;
import com.huawei.hianalytics.util.e;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class m implements a {
    private Context a;

    public m(Context context) {
        this.a = context;
    }

    private static void a(Context context) {
        SharedPreferences a = e.a(context, "global_v2");
        String str = (String) e.b(a, "uuid", "");
        if (TextUtils.isEmpty(str)) {
            str = UUID.randomUUID().toString().replace("-", "");
            com.huawei.hianalytics.e.a.a().f().k(str);
            a.edit().putString("uuid", str).apply();
        } else {
            com.huawei.hianalytics.e.a.a().f().k(str);
        }
        str = (String) e.b(a, "upload_url", "");
        long longValue = ((Long) e.b(a, "upload_url_time", Long.valueOf(0))).longValue();
        long currentTimeMillis = System.currentTimeMillis();
        if (TextUtils.isEmpty(str) || currentTimeMillis - longValue > HwAccountConstants.CHECK_SITE_COUNTRY_DURATION) {
            com.huawei.hianalytics.e.a.a().f().a(true);
            return;
        }
        try {
            JSONArray jSONArray = new JSONArray(str);
            int length = jSONArray.length();
            String[] strArr = new String[length];
            for (int i = 0; i < length; i++) {
                strArr[i] = "{url}/common/hmshioperbatch".replace("{url}", jSONArray.getString(i));
            }
            com.huawei.hianalytics.h.e.a().a("preload_url_tag", strArr);
            com.huawei.hianalytics.e.a.a().f().a(strArr);
            com.huawei.hianalytics.e.a.a().f().a(false);
        } catch (JSONException e) {
            b.c("InitInfoV2support", "cache pre url is error,need retrieve upload url ");
            com.huawei.hianalytics.e.a.a().f().a(true);
        }
    }

    private void a(Context context, String str) {
        Editor edit = h.d(context, str).edit();
        edit.clear();
        edit.apply();
    }

    private void a(SharedPreferences sharedPreferences) {
        Editor edit = sharedPreferences.edit();
        edit.clear();
        if (edit.commit()) {
            b.b("InitInfoV2support", "backup data is cleared");
            return;
        }
        b.b("InitInfoV2support", "backup data clear failed");
        edit.apply();
    }

    private void a(String str) {
        if (!TextUtils.isEmpty(str)) {
            try {
                JSONArray jSONArray = new JSONObject(str).getJSONArray("events");
                if (jSONArray == null || jSONArray.length() == 0) {
                    b.b("InitInfoV2support", "No V2CacheFile Data!");
                } else {
                    a(jSONArray);
                }
            } catch (JSONException e) {
                b.c("InitInfoV2support", "parseV2CacheData() eventJsonArray.cacheData No json !");
            }
        }
    }

    private void a(String str, String str2) {
        b(str);
        a(str2);
    }

    private void a(Map<String, ?> map) {
        String string = h.d(this.a, "global_v2").getString("request_id", "");
        if (!com.huawei.hianalytics.a.b.k()) {
            b.b("InitInfoV2support", "UnusualDataIgnored is false,begin handler unusualData,spKey: " + string);
            String[] split = string.split("#");
            String str = split[0];
            String str2 = split[1];
            c[] b = com.huawei.hianalytics.f.b.a.b(this.a, (String) map.get(string));
            if (b.length != 0) {
                new c(this.a, str, b, com.huawei.hianalytics.a.b.b(), str2).a();
            } else {
                b.b("InitInfoV2support", "backupUnusualData is empty");
            }
        }
        map.remove(string);
        b.b("InitInfoV2support", "remove this unusualData, backup data size : " + map.size());
    }

    private void a(Map<String, JSONArray> map, Map<String, ?> map2) {
        for (Entry entry : map2.entrySet()) {
            JSONArray jSONArray;
            String str = (String) entry.getKey();
            Object obj = str.split("#")[0];
            if (map.containsKey(obj)) {
                jSONArray = (JSONArray) map.get(obj);
            } else {
                jSONArray = new JSONArray();
                map.put(obj, jSONArray);
            }
            if (entry.getValue() instanceof String) {
                try {
                    JSONArray jSONArray2 = new JSONArray((String) entry.getValue());
                    for (int i = 0; i < jSONArray2.length(); i++) {
                        jSONArray.put(jSONArray2.getJSONObject(i));
                    }
                } catch (JSONException e) {
                    b.c("InitInfoV2support", "backup data is error! spKey: " + str);
                }
            }
        }
    }

    private void a(JSONArray jSONArray) {
        for (int i = 0; i < jSONArray.length(); i++) {
            try {
                JSONObject jSONObject = jSONArray.getJSONObject(i);
                if (jSONObject != null) {
                    Object optString = jSONObject.optString("event");
                    Object a = d.a(jSONObject.getString("content"), this.a);
                    Object optString2 = jSONObject.optString("eventtime");
                    String optString3 = jSONObject.optString("type");
                    if (TextUtils.isEmpty(optString) || TextUtils.isEmpty(a) || TextUtils.isEmpty(optString2) || TextUtils.isEmpty(optString3)) {
                        b.c("InitInfoV2support", "parseV2SPData() Data anomaly! Discarding data.");
                        return;
                    } else {
                        k.a(new d(this.a, "_default_config_tag", i.a(optString3), optString, a, Long.parseLong(optString2)));
                    }
                } else {
                    continue;
                }
            } catch (JSONException e) {
                b.c("InitInfoV2support", "parseV2SPData() eventJsonArray.getJSONObject() error !");
            }
        }
    }

    private void a(boolean z) {
        String b = com.huawei.hianalytics.a.b.b();
        String g = com.huawei.hianalytics.a.b.g();
        if (TextUtils.isEmpty(b)) {
            b.b("InitInfoV2support", "app ver is first save!");
        } else if (!b.equals(g)) {
            b.b("InitInfoV2support", "the appVers are different!");
            i.a().a("", this.a, "", b);
        } else if (z) {
            b.b("InitInfoV2support", "report backup data!");
            i.a().a("", this.a, "", g);
        }
    }

    private boolean a() {
        b.b("InitInfoV2support", "begin handler backup data...");
        SharedPreferences d = h.d(this.a, "backup_event");
        if (h.b(this.a, "backup_event") + h.b(this.a, "stat_v2_1") > 5242880) {
            b.c("InitInfoV2support", "backup data exceeds limit,clear it");
            a(d);
            return false;
        }
        Map all = d.getAll();
        if (all.size() == 0) {
            b.b("InitInfoV2support", "No backup data needed to be processed");
            return false;
        } else if (all.size() > WXMediaMessage.DESCRIPTION_LENGTH_LIMIT) {
            b.b("InitInfoV2support", "back up data too mach");
            a(d);
            return false;
        } else {
            a(d);
            a(all);
            Map hashMap = new HashMap();
            a(hashMap, all);
            SharedPreferences d2 = h.d(this.a, "stat_v2_1");
            boolean z = false;
            for (Entry entry : hashMap.entrySet()) {
                JSONArray jSONArray;
                String str = (String) entry.getKey();
                try {
                    jSONArray = new JSONArray(d2.getString(str, ""));
                } catch (JSONException e) {
                    b.b("InitInfoV2support", "handler unusualData: stat sp data is error,spKey: " + str);
                    jSONArray = new JSONArray();
                }
                for (int i = 0; i < ((JSONArray) entry.getValue()).length(); i++) {
                    try {
                        jSONArray.put(((JSONArray) entry.getValue()).getJSONObject(i));
                    } catch (JSONException e2) {
                        b.b("InitInfoV2support", "handler backup data,json exception");
                    }
                }
                z = true;
                try {
                    h.a(d2, str, jSONArray.toString());
                } catch (OutOfMemoryError e3) {
                    b.c("InitInfoV2support", "handler backup data to stat sp error : OOM");
                    z = false;
                }
            }
            return z;
        }
    }

    private void b() {
        if (h.c(this.a, "stat_v2").exists()) {
            SharedPreferences d = h.d(this.a, "stat_v2");
            if (d == null) {
                b.c("InitInfoV2support", "getV2StateSP error!");
                return;
            }
            String str = (String) h.b(d, "events", "");
            String a = d.a(com.huawei.hianalytics.f.g.e.a(this.a, "cached_v2"), this.a);
            c();
            if (TextUtils.isEmpty(str) && TextUtils.isEmpty(a)) {
                b.b("InitInfoV2support", " No cached V2 data found.");
                return;
            } else {
                a(str, a);
                return;
            }
        }
        b.b("InitInfoV2support", "No V2 data supporting!");
    }

    private void b(String str) {
        JSONArray jSONArray;
        try {
            if (!TextUtils.isEmpty(str)) {
                jSONArray = new JSONArray(str);
                if (jSONArray != null || jSONArray.length() == 0) {
                    b.b("InitInfoV2support", "No V2State Data!");
                } else {
                    a(jSONArray);
                    return;
                }
            }
        } catch (JSONException e) {
            b.c("InitInfoV2support", "parseV2SPData:When events turn to JSONArray,json Exception");
        }
        jSONArray = null;
        if (jSONArray != null) {
        }
        b.b("InitInfoV2support", "No V2State Data!");
    }

    private void c() {
        a(this.a, "stat_v2");
        com.huawei.hianalytics.f.g.e.b(this.a, "cached_v2");
    }

    private void d() {
        a(this.a);
    }

    public void run() {
        d();
        a(a());
        SharedPreferences d = h.d(this.a, "global_v2");
        if (((Boolean) h.b(d, "v2cacheHandlerFlag", Boolean.valueOf(false))).booleanValue()) {
            b.b("InitInfoV2support", "cached data by HASDKV2 has already handled.");
            return;
        }
        h.a(d, "v2cacheHandlerFlag", Boolean.valueOf(true));
        b();
    }
}
